import fs from 'fs/promises'
import fse from 'fs-extra'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))
export const config = readConfig()
export const rootDir = resolve(__dirname)
export const dataDir = resolve(rootDir, 'data')
export const srcDir = resolve(dataDir, 'src')
export const distDir = resolve(dataDir, 'dist')

const dirs = [srcDir, distDir]
dirs.forEach(dir => {
  fs.mkdir(dir, { recursive: true })
})

function readConfig() {
  const json = fse.readJsonSync('./config.json', { throws: false })

  return {
    ...json,
  }
}

export const saveconfig = () => {
  fse.writeJSONSync('./config.json', config)
}
